﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Common.Views;

namespace DataAccess
{
    public class OrdersRepository : ConnectionClass
    {
        public OrdersRepository() : base() { }

        public IEnumerable<Order> GetUserOrders(string username)
        {
            return entities.Orders.Where(o => o.Username_FK == username);
        }

        public IQueryable<OrderDetail> GetOrderDetails(Guid oID)
        {
            return entities.OrderDetails.Where(od => od.Order_FK == oID);
        }

        public void AddOrder(Order o)
        {
            entities.Orders.AddObject(o);
            entities.SaveChanges();
        }

        public void AddOrderDetails(OrderDetail od)
        {
            entities.OrderDetails.AddObject(od);
            entities.SaveChanges();
        }

        public IQueryable<UserOrderDetailsView> GetOrderDetailsByOrderID(Guid oID)
        {
            return (
                from od in entities.OrderDetails
                join o in entities.Orders on od.Order_FK equals o.ID
                join pr in entities.Products on od.Product_FK equals pr.ID
                where od.Order_FK == oID

                select new UserOrderDetailsView()
                {
                    OID = o.ID,
                    ID = od.ID,
                    ProductID = pr.ID,
                    ProductName = pr.Name,
                    Price = pr.Price * od.Quantity,
                    Quantity = od.Quantity,
                    ExpiryDate = o.Date
                }
                );
        }

        public IQueryable<UserOrderDetailsView> GetOrderDetailsByOrderIDandProductID(Guid oID, int pID)
        {
            return (
                from od in entities.OrderDetails
                join o in entities.Orders on od.Order_FK equals o.ID
                join pr in entities.Products on od.Product_FK equals pr.ID
                where od.Order_FK == oID && od.Product_FK == pID

                select new UserOrderDetailsView()
                {
                    OID = o.ID,
                    ID = od.ID,
                    ProductID = pr.ID,
                    ProductName = pr.Name,
                    Price = pr.Price * od.Quantity,
                    Quantity = od.Quantity,
                    ExpiryDate = o.Date
                }
                );
        }

        public IEnumerable<UserOrderDetailsView> GetOrderDetailsFromFaultNO(int tn)
        {
            return (
                           from od in entities.OrderDetails
                           join o in entities.Orders on od.Order_FK equals o.ID
                           join uf in entities.UserFaults
                           on od.Product_FK equals uf.Product_FK
                           join p in entities.Products
                           on uf.Product_FK equals p.ID
                           where uf.FaultID == tn

                           select new UserOrderDetailsView()
                           {
                               OID = o.ID,
                               ID = od.ID,
                               ProductID = p.ID,
                               ProductName = p.Name,
                               Price = p.Price * od.Quantity,
                               Quantity = od.Quantity,
                               ExpiryDate = o.Date
                           }
                           );
        }

    }
}
